package com.toolmanrln.logistics.config;

import com.google.code.kaptcha.Producer;
import com.google.code.kaptcha.impl.DefaultKaptcha;
import com.google.code.kaptcha.util.Config;
import com.toolmanrln.logistics.modules.user.service.impl.UserDetailsServiceImpl;
import com.toolmanrln.security.config.EasySecurityConfig;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.method.configuration.EnableGlobalMethodSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.core.userdetails.UserDetailsService;

import java.util.Properties;

/**
 * @author ToolMan.RLN
 * @since 2020-05-18 11:20
 */
@Configuration
@EnableWebSecurity
@EnableGlobalMethodSecurity(prePostEnabled = true)
public class UserSecurityConfig extends EasySecurityConfig {

    @Autowired
    private UserDetailsServiceImpl userDetailsService;

    private static final String SYMBOLS = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
    private static final String EASY = "0123456789";

    @Bean
    Producer verifyCode() {
        Properties properties = new Properties();
        properties.setProperty("kaptcha.image.width", "200");
        properties.setProperty("kaptcha.image.height", "80");
//        properties.setProperty("kaptcha.textproducer.char.string", SYMBOLS);
//        properties.setProperty("kaptcha.textproducer.char.length", "6");

        properties.setProperty("kaptcha.textproducer.char.string", EASY);
        properties.setProperty("kaptcha.textproducer.char.length", "2");
        Config config = new Config(properties);
        DefaultKaptcha defaultKaptcha = new DefaultKaptcha();
        defaultKaptcha.setConfig(config);
        return defaultKaptcha;
    }

    @Bean
    @Override
    public UserDetailsService userDetailsService() {
        //获取登录用户信息
        return username -> userDetailsService.loadUserByUsername(username);
    }
}
